<!DOCTYPE html>
<html>

<head>
    <title>VectorFieldAnim / ScalarField</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
    <link rel="stylesheet" href="//unpkg.com/leaflet@1.2.0/dist/leaflet.css" />
    <link rel="stylesheet" href="examples.css" />
    <link href="https://fonts.googleapis.com/css?family=Roboto:100,400" rel="stylesheet">
</head>

<body>
    <h1 class="title mapTitle">VectorFieldAnim > ScalarField</h1>
    <div id="map"></div>

    <!-- CDN -->
    <script src="//d3js.org/d3.v4.min.js"></script>
    <script src="//npmcdn.com/leaflet@1.2.0/dist/leaflet.js"></script>
    <script src="//cdnjs.cloudflare.com/ajax/libs/chroma-js/2.1.0/chroma.min.js"></script>

    <!-- Plugin -->
    <script src="dist/leaflet.canvaslayer.field.js"></script>

    <script>
        let map = L.map("map");

        // Basemap
        let url = 'https://cartodb-basemaps-{s}.global.ssl.fastly.net/dark_nolabels/{z}/{x}/{y}.png';
        L.tileLayer(url, {
            attribution: 'OSM & Carto',
            subdomains: 'abcd',
            maxZoom: 19
        }).addTo(map);

        // ScalarField derived from a Vectorfield
        d3.text('data/Atlantic_U.asc', function (u) {
            d3.text('data/Atlantic_V.asc', function (v) {
                const toMetersPerSecond = 0.001;
                const vf = L.VectorField.fromASCIIGrids(u, v, toMetersPerSecond);

                // a) First derived field: Magnitude (m/s)
                const s = vf.getScalarField('magnitude'); // << derived ScalarField
                /*  custom scale, based on 'earth.nullschool.net'
                    (example:  https://earth.nullschool.net/#current/ocean/surface/currents/overlay=currents/equirectangular=-11.95,29.62,1112)
                */
                const magnitude = L.canvasLayer.scalarField(s, {
                    color: chroma.scale(
                        ['#1F263A', '#414AA9', '#44758C', '#399B58', 'DCD296', 'F2E899', 'A53E3C', '9C3333'], [.1, .2, .3, .4, .7, .9, 1.5, 2]
                    ),
                    opacity: 0.65
                });

                // b) Second derived field: DirectionFrom (º): (0 to 360º) | N is 0º and E is 90º
                const direction = L.canvasLayer.scalarField(
                    vf.getScalarField('directionFrom'), {
                        type: 'vector',
                        color: 'white',
                        vectorSize: 25,
                        arrowDirection: 'from'
                    });

                const animation = L.canvasLayer.vectorFieldAnim(vf, {
                    paths: 5000,
                    fade: 0.97,
                    maxAge: 100,
                    velocityScale: 1 / 40,
                    color: 'rgba(255, 255, 255, 0.7)'
                }).addTo(map);

                L.control.layers({}, {
                    "Vector animation": animation,
                    "Derived magnitude": magnitude,
                    "Derived direction": direction,
                }, {
                        position: 'bottomleft',
                        collapsed: false
                    }).addTo(map);


                map.fitBounds(animation.getBounds());

                magnitude.on('click', function (e) {
                    if (e.value !== null) {
                        let v = e.value.toFixed(2);
                        let html = (`<span class="popupText">magnitude: ${v} m/s</span>`);
                        let popup = L.popup().setLatLng(e.latlng).setContent(html).openOn(map);
                    }
                });

                direction.on('click', function (e) {
                    if (e.value !== null) {
                        let v = e.value.toFixed(0);
                        let html = (`<span class="popupText">direction: ${v}º</span>`);
                        let popup = L.popup().setLatLng(e.latlng).setContent(html).openOn(map);
                    }
                });
            });
        });
    </script>
</body>

</html>